﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;

namespace FileClient
{
    public class Network
    {

        private Socket clientSock;
        private int bufferSize;
        private static Network networkInstance = null;
        private Thread listener;
        private bool running = false;

        private Network()
        {
            bufferSize = 1000;
        }

        public static Network Instance
        {
            get
            {
                if (networkInstance == null)
                    networkInstance = new Network();
                return networkInstance;
            }
        }

        public void connect(String ip)
        {
            IPAddress[] ipAddress = Dns.GetHostAddresses(ip);
            IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
            clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            clientSock.Connect(ipEnd);
            running = true;
            listener = new Thread(new ThreadStart(this.recMess));
            listener.Start();
        }

        public void disconnect()
        {
            Protocol.Instance.Disconnect();
            Instance.running = false;            
        }

        public void sendMessage(string message)
        {
            byte[] sendMess = Encoding.UTF8.GetBytes(message);

            System.Diagnostics.Debug.WriteLine("Message sent: " + message);

            clientSock.Send(sendMess);
        }

        public void sendfile(string message, string filePath, string fileName)
        {
            try
            {
                sendMessage(message);

                byte[] buffer;
                FileStream neofiledata = new FileStream(filePath + fileName, FileMode.Open, FileAccess.Read);

                buffer = new byte[bufferSize];
                int count;

                while ((count = neofiledata.Read(buffer, 0, bufferSize)) > 0)
                {
                    System.Diagnostics.Debug.WriteLine("Bytes Sent: " + clientSock.Send(buffer, count, SocketFlags.None));
                }

                System.Diagnostics.Debug.WriteLine("file: {0} has been sent", fileName);
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err);
            }
        }

        private void recMess()
        {
            //System.Diagnostics.Debug.WriteLine("Thread entered");
            byte[] buffer = new byte[1024];

            while (Instance.running)
            {
                clientSock.Receive(buffer);
                Protocol.Instance.messageHandler(Encoding.UTF8.GetString(buffer));
                System.Diagnostics.Debug.WriteLine("Message Recived: " + Encoding.UTF8.GetString(buffer));
                //return Encoding.UTF8.GetString(buffer);
            }
            //System.Diagnostics.Debug.WriteLine("Thread exited");

            Instance.clientSock.Close();
        }
    }
}
